package cn;

import javax.swing.*;
import java.awt.*;

public class EnemyPlane extends Thread{
    public GameFrame gf;
    int x=15,y;
    int width=45,hight=45;
    int speed=3;

    public Image img=new ImageIcon("img/敌机.png").getImage();

    public EnemyPlane(int x, int y, GameFrame gf) {
        this.x = x;
        this.y = y;
       this.gf=gf;
    }

    //  玛丽飞翔的逻辑
    public void run(){
        while(true){
            //  碰撞到了
            if (hit()){
                this.speed=0;
                this.img= new ImageIcon("img/爆炸.png").getImage();
                try {
                    Thread.sleep(300    );
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                gf.enemys.remove(this);
                //  被子弹击毁
                break;
            }
            if (this.y>=760){
                break;
            }
            try {
                Thread.sleep(10    );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //  检测碰撞
    public boolean hit(){
        Rectangle myrect=new Rectangle(this.x,this.y,this.width,this.hight);
        Rectangle rect = null;
        for (int i = 0; i < gf.bullets.size(); i++) {
            Bullet bullet = gf.bullets.get(i);
            //System.out.println("test hit");
            rect = new Rectangle(bullet.x,bullet.y-1,bullet.width,bullet.hight);
            //  碰撞检测
            if(myrect.intersects(rect)){
                return true;
            }
        }
        return false;
    }


}
